/** row-switch start **/
function onlistUpdate(obj, utype='sw') {
    data = JSON.parse(obj.val());
    if(utype == 'sw')
    {
        $.each(data, function(k, v) {
            if (k != obj.attr('parimy-key')) {
                if (v == 0) {
                    data[k] = 1;
                } else {
                    data[k] = 0;
                }
            }
        });
    }
    else if(utype == 'radio') {
        udata = [];
        $.each(data, function(k, v) {
            udata[v.name] = v.value;
        });
        data = udata;
    }
    // console.log(data);
    // return false;
    $(this).request('onUpdate', {
        data: data,
        beforeUpdate: function() {
            obj.val(JSON.stringify(data));
            return true;
        },
    });
}
/** row-switch&radio end **/

/** row-checkbox start **/
function onUpdateChecklist(that)
{
    var field = that.parents('.field-base-params');
    var params = field.find('.form-check input');
    var field_values = [];
    var data = {};
    $.each(params, function(i, item){
        if(item.checked==true) field_values.push($(item).val());
    });
    data[field.attr('primary-name')] =  field.attr('primary-value');
    data[field.data('field')] = field_values.join(',');

    $(this).request('onUpdate', {data: data});
}
/** row-checkbox end **/

/** row-edit start **/
function permutationInput(t) {

    let $this = $(t);
    let data = JSON.parse($this.attr('data-default'));
    $this.parent().hide();
    createInputblock($this, data);
}

function createInputblock(obj, data) {
    $('.edit-block').remove();
    $('.custom-edit .field_text').show();
    obj.parents('.custom-edit').find('.field_text').hide();

    let input_id = obj.attr('input-id');
    let block_str = `<div class='form-inline edit-block'>
                        <input id="` + input_id + `" class="form-control edit-input" type='text' />
                        <div class="btn-group">
                            <button type="button" onclick="onEditUpdate($('#` + input_id + `'))" class="btn btn-primary btn-sm" title="保存">
                                <i class="icon-check"></i>
                            </button>
                            <button type="button" class="btn btn-danger btn-sm edit-close" onclick="closeBlock()" title="取消">
                                <i class="icon-close"></i>
                            </button>
                        </div>
                    </div>`;
    let block = $(block_str);
    block.find('.edit-input').attr('data', JSON.stringify(data));
    $.each(data, function(i, item) {
        if (item.name != block.find('.edit-input').attr('primary')) {
            block.find('.edit-input').attr('name', item.name);
            block.find('.edit-input').val(item.value);
        }
    });

    obj.parents('.custom-edit').append(block);
}

function closeBlock() {
    $('.custom-edit .field_text').show();
    $('.edit-close').parents('.edit-block').remove();
}

function onEditUpdate(obj) {
    var initdata = JSON.parse(obj.attr('data'));
    var data = {};
    var isRequest = true;
    $.each(initdata, function(i, item) {
        if (item.name == obj.attr('name')) {
            data[item.name] = obj.val();
            if (obj.val() != item.value) {
                initdata[i]['value'] = obj.val();
                obj.parents('.custom-edit').find('.field_text span').text(obj.val());
                obj.parents('.custom-edit').find('.field_text a').attr('data-default', JSON.stringify(initdata));
            } else {
                isRequest = false;
            }
        } else {
            data[item.name] = item.value;
        }
    });

    if (isRequest) {
        $(this).request('onUpdate', {
            data: data,
            complete: function() {
                closeBlock();
            }
        });
    } else {
        $.wn.flashMsg({
            'text': '您没有做任何修改.',
            'class': 'error',
            'interval': 3
        });
        closeBlock();
    }

}
/** row-edit end **/
